/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.gui;

import java.awt.Desktop;
import java.awt.Frame;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import program.beans.Bill;
import program.beans.BillAmount;
import program.beans.Category;
import program.beans.Company;
import program.database.Database;
import program.gui.model.AmountTableModel;
import program.gui.model.CategoryComboBoxModel;

/**
 *
 * Responsible: Patrick, Bernhard
 */
public class ViewBillDlg extends javax.swing.JDialog {

    /**
     * Creates new form NewBill
     */
    private Bill b;
    public ViewBillDlg(Frame parent, boolean modal, Bill b) {
        
        super(parent, modal);
        initComponents();
        this.b = b;
        if (b.getType() == 0) {
            pnMain.setBorder(new TitledBorder("Ausgaben"));
        } else {
            pnMain.setBorder(new TitledBorder("Einnahmen"));
        }

        tfCompany.setText(b.getComp().getName());
        tfCat.setText(b.getCat().getDescription());
        tfBrutto.setText(b.getAllBruttoAmountsFormated());
        tfNetto.setText(b.getAllNettoAmountsFormated());
        tfAddDate.setText(b.getBid());
        tfDate.setText(b.getBillDate());

        ArrayList<BillAmount> amounts = b.getAmounts();
        AmountTableModel atm = new AmountTableModel(amounts);
        tbAmounts.setModel(atm);

        System.out.println(b.getBillCopy());
        if (b.getBillCopy() == null) {
            btBC.setEnabled(false);
            
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        pnMain = new javax.swing.JPanel();
        lbFirma = new javax.swing.JLabel();
        lbDatum = new javax.swing.JLabel();
        tfCompany = new javax.swing.JTextField();
        pnSumme = new javax.swing.JPanel();
        lbBrutto = new javax.swing.JLabel();
        lbNetto = new javax.swing.JLabel();
        tfBrutto = new javax.swing.JTextField();
        tfNetto = new javax.swing.JTextField();
        btBC = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tfCat = new javax.swing.JTextField();
        tfDate = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbAmounts = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        tfAddDate = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        btOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Neue Rechnungen");
        setResizable(false);
        getContentPane().setLayout(new java.awt.BorderLayout(5, 5));

        pnMain.setBorder(javax.swing.BorderFactory.createTitledBorder("Einnahmen"));

        lbFirma.setText(" Firma:");

        lbDatum.setText("Rechnungsdatum:");

        tfCompany.setEditable(false);

        pnSumme.setBorder(javax.swing.BorderFactory.createTitledBorder("Summe"));

        lbBrutto.setText("Brutto:");

        lbNetto.setText("Netto:");

        tfBrutto.setEditable(false);
        tfBrutto.setText("               ");

        tfNetto.setEditable(false);
        tfNetto.setText("               ");

        javax.swing.GroupLayout pnSummeLayout = new javax.swing.GroupLayout(pnSumme);
        pnSumme.setLayout(pnSummeLayout);
        pnSummeLayout.setHorizontalGroup(
            pnSummeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnSummeLayout.createSequentialGroup()
                .addGroup(pnSummeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnSummeLayout.createSequentialGroup()
                        .addComponent(lbBrutto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfBrutto, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))
                    .addGroup(pnSummeLayout.createSequentialGroup()
                        .addComponent(lbNetto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfNetto)))
                .addContainerGap())
        );
        pnSummeLayout.setVerticalGroup(
            pnSummeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnSummeLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(pnSummeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfBrutto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbBrutto))
                .addGap(10, 10, 10)
                .addGroup(pnSummeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfNetto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbNetto)))
        );

        btBC.setText("Rechnungskopie");
        btBC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onBillcopy(evt);
            }
        });

        jLabel1.setText("Kategorie:");

        tfCat.setEditable(false);

        tfDate.setEditable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Beträge"));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tbAmounts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbAmounts);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jLabel2.setText("Hinzugefügt am:");

        tfAddDate.setEditable(false);

        javax.swing.GroupLayout pnMainLayout = new javax.swing.GroupLayout(pnMain);
        pnMain.setLayout(pnMainLayout);
        pnMainLayout.setHorizontalGroup(
            pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnMainLayout.createSequentialGroup()
                        .addComponent(pnSumme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btBC, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnMainLayout.createSequentialGroup()
                        .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbFirma)
                            .addComponent(lbDatum))
                        .addGap(36, 36, 36)
                        .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfCompany, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                            .addComponent(tfDate))
                        .addGap(51, 51, 51)
                        .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfCat)
                            .addComponent(tfAddDate, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                        .addGap(80, 80, 80)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnMainLayout.setVerticalGroup(
            pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbFirma)
                    .addComponent(tfCompany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(tfCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbDatum)
                    .addComponent(tfDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(tfAddDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnSumme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btBC))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        getContentPane().add(pnMain, java.awt.BorderLayout.CENTER);

        jPanel6.setLayout(new java.awt.GridBagLayout());

        btOk.setText("Fertig");
        btOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onFinish2(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 10, 20);
        jPanel6.add(btOk, gridBagConstraints);

        getContentPane().add(jPanel6, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onFinish2(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onFinish2
        dispose();

    }//GEN-LAST:event_onFinish2

    private void onBillcopy(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onBillcopy
        try {
            Desktop.getDesktop().open(b.getBillCopy());
        } catch (IOException ex) {
                       JOptionPane.showMessageDialog(null, ex);

        }
    }//GEN-LAST:event_onBillcopy


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBC;
    private javax.swing.JButton btOk;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbBrutto;
    private javax.swing.JLabel lbDatum;
    private javax.swing.JLabel lbFirma;
    private javax.swing.JLabel lbNetto;
    private javax.swing.JPanel pnMain;
    private javax.swing.JPanel pnSumme;
    private javax.swing.JTable tbAmounts;
    private javax.swing.JTextField tfAddDate;
    private javax.swing.JTextField tfBrutto;
    private javax.swing.JTextField tfCat;
    private javax.swing.JTextField tfCompany;
    private javax.swing.JTextField tfDate;
    private javax.swing.JTextField tfNetto;
    // End of variables declaration//GEN-END:variables
}
